Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Abstraction in Java

  • Data Abstraction is the property that allows only the most important details to be displayed to the user. The user is not shown the trivial or non-essential units. For example, a car is regarded as a whole rather than its individual components.
  • Data abstraction is also defined as the process of identifying only the necessary attributes of an entity while ignoring extraneous aspects. An object's qualities and behaviors distinguish it from other items of a similar type and aid in classifying/grouping the things.


Table Of Content

  • Abstraction in Java
  • Advantages of Abstraction
  • Abstract classes
  • Abstract Method in Java
  • Different scenario of Abstract class
  • Difference Between Encapsulation and Data Abstraction






  • For example, sending SMS, where you type the message and send it. You are not aware of the internal operations involved in delivery of message .
  • Abstraction allows you to focus on what the object does rather than how it does it.
  • Interfaces and abstract classes are used in Java to achieve complete abstraction. Using interfaces, we can achieve 100% abstraction.

Advantages of Abstraction

  • Abstraction simplifies the process of viewing things.
  • Abstraction Reduces code duplication and improves reusability.
  • Only essential details are provided to the user, it helps to increase the security of an application or program.
  • Abstraction improves application maintainability.
  • Abstraction improves the application's modularity.
  • The enhancement will be very simple in abstraction because we will be able to make changes to our internal system without affecting end users.

There are 2 way to achieve Abstraction

  • Abstract class (0 to 100%)
  • Interface (100%)




Abstract classes

An abstract class is one that has been declared to be abstract. It can support both abstract and non-abstract methods. It needs to be expanded and its method put into action. It cannot be created.


Important point about Abstract classes
  • An abstract class is one that has been declared with the abstract keyword.
  • All abstract methods may or may not be present in an abstract class. Some of them may be concrete procedures.
  • An abstract keyword must be used to declare any class that contains one or more abstract methods.
  • An abstract method are those methods that is declared but not implemented.
  • An abstract class cannot have an object. That is, an abstract class cannot be instantiated directly with the new operator.
  • An abstract class can have parameterized constructors, and it always has the default constructor.



Syntax

 abstract class Exam{}   


Abstract classes
 // Java Abstract classes example  
abstract class nano{
	abstract void Speed();
	  public void Weight() {
		   System.out.println("250 Kg");
	  }
}
class Vehicles extends nano{    
	@Override
	 void Speed() {
		System.out.println("90 kmph");	
	 }  
}
class Main{
	public static void main(String[] args) {
		Vehicles vehicles=new Vehicles();
		vehicles.Speed();
		vehicles.Weight();
	}
}



Output:

90 kmph 
250 Kg 


Abstract Method in Java

An abstract methods are those methods that is declared with abstract keyword but does not have implementation.


Syntax

 abstract void Exam();  // does not have implementation  



Java Abstract Method
abstract class Bike{
	abstract void MaxSpeed();
}
class Hero extends Bike{    
	@Override
	 void MaxSpeed() {
		System.out.println("Max Speed is : 140 kmph");	
	 }  
}
class Main{
	public static void main(String[] args) {
		Hero vehicles=new Hero();
		vehicles.MaxSpeed();
	}
}



Output:

Max Speed is : 140 kmph 


Different scenario of Abstract class


Case 1

Java permits constructors to be present in abstract classes. When an instance of an inherited class is produced, a constructor of an abstract class is also invoked.


above concept is describe in below example
abstract class Bike{
	public Bike() {
		System.out.println("Bike Class Constructor (Base Class)");
	}
	abstract void MaxSpeed();
}
class Hero extends Bike{    
	 public Hero(){
		 System.out.println("Hero Class Constructor (Derived Class)");
	 }
	@Override
	 void MaxSpeed() {
		System.out.println("Max Speed is : 140 kmph");	
	 }  
}
class Main{
	public static void main(String[] args) {
		Hero hero=new Hero();
		hero.MaxSpeed();
	}
}



Output:

Bike Class Constructor (Base Class) 
Hero Class Constructor (Derived Class) 
Max Speed is : 140 kmph 


Case 2

Java allows us to have abstract classes without abstract methods. This enables us to develop classes that can only be inherited rather than instantiated.


above concept is describe in below example
abstract class Bike{ //abstract class without abstract method
    void MaxSpeed() {
		System.out.println("Max Speed is : 140 kmph");	
	 } 
}
class Hero extends Bike{    
	//This Hero class only inherits the Bike (Base) class methods
}
class Main{
	public static void main(String[] args) {
		Hero hero=new Hero();
		hero.MaxSpeed();
	}
}



Output:

Max Speed is : 140 kmph 


Case 3

If the Child class is unable to implement all of the Parent class's abstract methods, we should declare that Child class abstract so that the next level Child class can implement the remaining abstract method.


above concept is describe in below example
abstract class Bike{
	abstract void MaxSpeed1();
	abstract void MaxSpeed2();
	abstract void MaxSpeed3();	
}
abstract class Hero extends Bike{   // removing abstract from hero class will cause error 
                                                             // to implement bike abstract method
	@Override
	 void MaxSpeed1() {
		System.out.println("Max Speed is : 140 kmph");	
	 }
}
class Honda extends Hero{    
	@Override
	 void MaxSpeed2() {
		System.out.println("Max Speed is : 100 kmph");	
	 }  
	@Override
	 void MaxSpeed3() {
		System.out.println("Max Speed is : 80 kmph");	
	 }  
}
class Main{
	public static void main(String[] args) {
		Honda honda=new Honda();
		honda.MaxSpeed1();
		honda.MaxSpeed2();
		honda.MaxSpeed3();
	}
}



Output:

Max Speed is : 140 kmph 
Max Speed is : 100 kmph 
Max Speed is : 80 kmph 


Case 4

The abstract keyword can be used to declare both top-level classes (Outer classes) and inner classes as abstract.


above concept is describe in below example
abstract class Bike{
	abstract class splender{
		abstract void MaxSpeed1();
	}
}
class Hero extends Bike{   
    class brand extends splender{  // innner class brand
	   	 void MaxSpeed1() {
	 		System.out.println("Max Speed is : 140 kmph");	
	 	 }
    }
}
class Main{
	public static void main(String[] args) {
		Hero hero=new Hero();   // Instantiating outer class
		Hero.brand herobrand =hero.new brand(); // Instantiating inner class
		herobrand.MaxSpeed1();
	}
}



Output:

Max Speed is : 140 kmph 


Case 5

In an abstract class, we can define static methods that can be called without the need for an object.


above concept is describe in below example
abstract class Bike{
	abstract class splender{
		abstract void MaxSpeed1();
	}
    static void StaticCheck(){ //  Static method in abstract class
        System.out.println("I am Static method  from abstract class");
    }
}
class Hero extends Bike{   
    class brand extends splender{  // innner class brand
	   	 void MaxSpeed1() {
	 		System.out.println("Max Speed is : 140 kmph");	
	 	 }
    }
}
class Main{
	public static void main(String[] args) {
		Hero hero=new Hero();   // Instantiating outer class
		Hero.brand herobrand =hero.new brand(); // Instantiating inner class
		herobrand.MaxSpeed1();
		Bike.StaticCheck(); // static method called without object creation
	}
}



Output:

Max Speed is : 140 kmph 
I am Static method  from abstract class 


Case 6

In an abstract class, we can also have final methods but that methods cannot be overridden and abstract calss can not have object if created it will cause C.T Error.


above concept is describe in below example
abstract class Bike{
	
	final void MaxSpeed(){
 		System.out.println("Max Speed is : 140 kmph");	
 	 }   
}
class Hero extends Bike{   
  
}
class Main{
	public static void main(String[] args) {
		
	//	Bike bike=new Bike() // C.T Errror Bike
		
		Bike hero=new Hero();   // Instantiating hero class
		hero.MaxSpeed();		
	
	}
}



Output:

Max Speed is : 140 kmph 

Comparison Chart

Difference Between Encapsulation and Data Abstraction are mention below


S.N Abstraction Encapsulation
1. The process or method of obtaining information is known as abstraction. The process or method of containing information is known as encapsulation.
2. The class information is used by Early Binding to resolve method calling. Late Binding resolves method calls using the object.
3. Problems are resolved at the design or interface level in abstraction. Problems are resolved at the implementation level in encapsulation.
4. The abstraction-supporting objects are encapsulated. It is not necessary to abstract the encapsulated objects.
5. Access to a specific portion of data is made possible by abstraction. Encapsulation conceals data so that the user cannot directly access it (data hiding).
6. Abstraction is a technique for hiding unwanted information. Encapsulation is a technique that encapsulated data in a single entity or unit and protects data from the outside world.
7. The use of abstract classes and interfaces allows us to implement abstraction. Encapsulation can be implemented using the private, protected, and public access modifiers.
8. In abstraction, "what" sould to be done is the main focus. The emphasis in encapsulation is on "How" it should be done.
9. With abstraction, abstract classes and interfaces are used to hide implementation complexities. In encapsulation, the data is concealed using getter and setter methods.


Java Abstraction Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Abstraction
Java Interface
Abstract class vs Interface
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.